﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OutOfMemoryException
{
    public delegate void SAMHandlerEventHandler(string eventType, string param);
    public partial class Form1 : Form
    {

        public event SAMHandlerEventHandler OnStepping;

        List<string> list = new List<string>();
        public Form1()
        {
            InitializeComponent();
            OnStepping += M_SAMHandlerSequence_OnStepping;
        }

        private void M_SAMHandlerSequence_OnStepping(string eventType, string param)
        {
            MethodInvoker me = delegate ()
            {
                label1.Text = param;
            };
            this.BeginInvoke(me, null);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread thrMain = new Thread(new ThreadStart(Test));
            thrMain.IsBackground = true;
            thrMain.Start();
        }

        List<Person> listPerson = new List<Person>();

        private void Test()
        {
            try
            {
                int i = 0;
                while (true)
                {
                    var date = DateTime.Now.ToString();
                    //var content = "任职期间，他负责开展联合国难民署的深刻结构性改革，日内瓦总部的工作人员减少了20%以上，提高了难民署的成本效益和应急能力。在其任职期间，推出了以需求为准的预算编制法，因冲突和迫害而流离失所的人数从2005年的3800万人增加到2015年的6000万人，难民署的活动量也增长三倍。古特雷斯先生任职期间，出现了几十年来一些最大规模的流离失所危机，特别是叙利亚和伊拉克的冲突，还有南苏丹、中非共和国和也门的危机。";
                    //list.Add(content + content + content + content + content + content + content);

                    Person person = new Person();
                    person.Name = i.ToString();
                    person.Age = i;

                    listPerson.Add(person);

                    // List<Person> list = new List<Person>();

                    Application.DoEvents();
                    Thread.Sleep(10);
                    OnStepping?.BeginInvoke("Time", date, null, null);
                    i = i + 1;
                    LogHelper.WriteLog("count:" + i);
                }
            }
            catch (Exception ex)
            {
                MethodInvoker me = delegate ()
                {
                    label2.Text = ex.Message;
                };
                this.BeginInvoke(me, null);

                LogHelper.WriteLog("Message:" + ex.Message);
            }
        }

    }
}
